<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 标签系统
 * 
 * @desc    标签系统，管理网站的所有标签，比如：用户喜好特长，商户产品主打，评论打标签等。
 * @version 1.0.0 
 * @author  Jason
 * @date    2017年7月3日 下午1:25:10
 */
class Mod_tag extends MY_Model
{
	use models\common\TreeStructure; // 注意 before_treeStructure_create_data_handler
	use models\common\BinaryBitBasedStatus;
	
	protected $table_mapper = [
			'master' => 'tag',
			'relation' => 'tag_relation',
	];
	
	protected $allowed_fields = [
			'master' => ['module', 'parent_id', 'name', 'level', 'position', 'language', 'sort_order', 'status'],
			'relation' => ['source_type', 'source_id', 'source_group_id', 'tag_id', 'tag_module', 'tag_language'],
	];
	
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		
		$this->status_bit_config = [
				'master' => [
						// 状态：0不显示1显示
						'status_bit_length' => 1,
						'status_bit_positive_tags' => ['enabled'],
						'status_bit_negative_tags' => ['disabled'],
				],
		];
	}
	
	/**
	 * 插入前处理器
	 * 
	 * @param array  $data
	 * @param string $mapping	表映射
	 * 
	 * @return array
	 */
	public function before_create_data_handler($data, $mapping = 'master')
	{
		if ($mapping == 'master') {
			$data = $this->before_treeStructure_create_data_handler($data, $mapping);
			if (isset($data['status_tags'])) {
				if (empty($data['status_tags'])) {
					$data['status_tags'] = [];
				} else if (!is_array($data['status_tags'])) {
					$data['status_tags'] = explode(',', $data['status_tags']);
				}
				$data['status'] = $this->calculate_status_by_tags($data['status_tags']);;
			} else if (!isset($data['status'])) {
				// 默认先启用，即不用审核
				$data['status'] = 1;
			}
		}
		
		return $data;
	}
	
	/**
	 * 更新前处理器
	 * 
	 * @desc  主要负责格式化一些字段 & 过滤掉多语言字段，确保只在默认语言下更改主表的多语言字段
	 * @param number $id
	 * @param array  $data
	 * @param string $mapping	表映射
	 * 
	 * @return array
	 */
	public function before_update_data_handler($id, $data, $mapping = 'master')
	{
		if ($mapping == 'master') {
			$data = $this->before_treeStructure_create_data_handler($data, $mapping);
			if (isset($data['status_tags'])) {
				if (empty($data['status_tags'])) {
					$data['status_tags'] = [];
				} else if (!is_array($data['status_tags'])) {
					$data['status_tags'] = explode(',', $data['status_tags']);
				}
				$data['status'] = $this->calculate_status_by_tags($data['status_tags']);;
			}
		}
		
		return $data;
	}
	
	public function has_tag_exists($module, $parent_id, $language, $name)
	{
		$conditions = [
				['select', 'id'],
				['where', [[
							'module' => $module, 
							'parent_id' => $parent_id, 
							'name' => $name,
						]]
				],
				['limit', 1],
		];
		if (!empty($language)) {
			$conditions[] = ['where', [['language' => $language]]];
		}
		$tags = $this->select_instances_by_conditions($conditions);
		$tag = array_pop($tags);
		
		$has = !empty($tag['id']);
		return [$has, $tag];
	}
	
	/**
	 * 设置一条标签关系
	 * 
	 * @desc    只设置一条
	 * @author  Jason
	 * @date    2017年7月7日 下午12:13:56
	 * 
	 * @param   string $source_type		关系的资源类型
	 * @param   number $source_id		关系的资源ID
	 * @param   number $source_group_id	关系的资源分组ID
	 * @param   array  $tag				标签信息，须包含id,module,language三个字段
	 * 
	 * @return  array	array($status, $content)
	 */
	public function set_relation($source_type, $source_id, $source_group_id, $tag)
	{
		$data = [
				'source_type' => $source_type,
				'source_id' => $source_id,
				'source_group_id' => $source_group_id,
				'tag_id' => $tag['id'],
				'tag_module' => $tag['module'],
				'tag_language' => $tag['language'],
		];
		
		list($status, $content) = $this->create_instance($data, 'relation');
		if ( ! $status) {
			$content = ['tag_msg_create_relation_error'];
		}
		return [$status, $content];
	}
	
}